package com.moviesearch.elastic7.service.impl;

import com.moviesearch.elastic7.mapper.FilmresourcesMapper;
import com.moviesearch.elastic7.service.ElasticIndexService;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.settings.Settings;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
@Service
public class ElasticIndexServiceImpl implements ElasticIndexService {
    @Resource
    private RestHighLevelClient restHighLevelClient;

    /*利用mapper创建一个index*/
    public String createIndex(String index) throws IOException {
        CreateIndexRequest createIndexRequest = new CreateIndexRequest(index);
        createIndexRequest.settings(Settings.builder()
                .put("index.number_of_shards", 3)
                .put("index.number_of_replicas", 2)
        );
        Map<String, Object> mapping = new HashMap<>();
        Map<String, Object> properties = new HashMap<>();

        Map<String, Object> id = new HashMap<>();
        id.put("type", "keyword");

        Map<String, Object> filmtitle = new HashMap<>();
        filmtitle.put("type", "text");
        filmtitle.put("analyzer", "ik_max_word");

        Map<String, Object> filmactors = new HashMap<>();
        filmactors.put("type", "text");
        filmactors.put("analyzer", "ik_max_word");

        Map<String, Object> filmscoring = new HashMap<>();
        filmscoring.put("type", "keyword");

        Map<String, Object> numberofevaluators = new HashMap<>();
        numberofevaluators.put("type", "keyword");

        Map<String, Object> filmdescription = new HashMap<>();
        filmdescription.put("type", "text");
        filmdescription.put("analyzer", "ik_max_word");

        Map<String, Object> filmranking = new HashMap<>();
        filmranking.put("type", "keyword");

        Map<String, Object> createtime = new HashMap<>();
        createtime.put("type", "keyword");

        Map<String, Object> updatetime = new HashMap<>();
        updatetime.put("type", "keyword");

        properties.put("id",id);
        properties.put("filmtitle",filmtitle);
        properties.put("filmactors",filmactors);
        properties.put("filmscoring",filmscoring);
        properties.put("numberofevaluators",numberofevaluators);
        properties.put("filmdescription",filmdescription);
        properties.put("filmranking",filmranking);
        properties.put("createtime",createtime);
        properties.put("updatetime",updatetime);

        mapping.put("properties",properties);

        createIndexRequest.mapping(mapping);
        //同步执行
        restHighLevelClient.indices().create(createIndexRequest, RequestOptions.DEFAULT);
        //异步执行
        //restHighLevelClient.indices().createAsync(createIndexRequest, RequestOptions.DEFAULT, listener);
        return "success";
    }

    /*判断一个index是否存在*/
    public boolean  hasIndex(String index) throws IOException {
        GetIndexRequest request = new GetIndexRequest(index);
        boolean exists = restHighLevelClient.indices().exists(request, RequestOptions.DEFAULT);
        return exists;
    }

    @Override
    public String deleteIndex(String index) throws IOException {
        DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest(index);
        restHighLevelClient.indices().delete(deleteIndexRequest,RequestOptions.DEFAULT);
        return "success";
    }
}
